AppMenu.js ➔ rebuildMenu   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
const { Menu } = require('electron');
2
const menuStructure = require("./data/menu.json");
0 ignored issues
show
Unused Code introduced by
The constant menuStructure seems to be never used. Consider removing it.
Loading history...
3
const _ = require("lodash");
0 ignored issues
show
Unused Code introduced by
The constant _ seems to be never used. Consider removing it.
Loading history...
4
5
module.exports = class AppMenu {
6
    constructor(app) {
7
        this.app = app;
8
        this.rebuildMenu();
9
10
        app.store.onDidChange('recentDocs', this.onRecentDocsChange.bind(this));
11
    }
12
13
    // Determines if a menu item can completely skip check or not
14
    doMenuItemSkip(obj) {
15
        // Is it not the wrong platform
16
        if (obj.platform !== undefined &&
17
            process.platform !== obj.platform)
18
            return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
19
20
        if (obj.notPlatform !== undefined &&
21
            process.platform === obj.notPlatform)
22
            return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
23
24
        // Is it the wrong build
25
        if (obj.env === "dev" &&
26
            this.app.isDev !== true)
27
            return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
28
29
        if (obj.env === "prod" &&
30
            this.app.isDev === true)
31
            return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
32
33
        return false;
34
    }
35
36
    _rebuildMenu(arr) {
37
        for (var i = 0; i < arr.length; i++) {
38
            const obj = arr[i];
39
40
            // Is not designated platform
41
            if (this.doMenuItemSkip(obj)) {
42
                arr.splice(i, 1);
43
                i--;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
44
                continue;
45
            }
46
47
            if (obj.placeholder !== undefined) {
48
                arr.splice(i, 1);
49
                i--;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
50
51
                if (obj.placeholder === "recentList")
52
                    arr.splice(i + 1, 0, ...this.recentFilesStructure);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
53
54
                continue;
55
            }
56
57
            if (obj.trigger !== undefined) {
58
                obj.click = () => {
59
                    if (Array.isArray(obj.triggerData))
60
                        this.app.emit(`menu-${obj.trigger}`, ...obj.triggerData);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
61
                    else
62
                        this.app.emit(`menu-${obj.trigger}`, obj.triggerData);
63
                }
64
            }
65
66
            if (obj.submenu !== undefined) {
67
                this._rebuildMenu(obj.submenu);
68
            }
69
        }
70
    }
71
72
    get recentFilesStructure() {
73
        const store = this.app.store;
74
        const structure = [];
75
76
        // Grab recent docs list
77
        const recentDocs = store.get('recentDocs', []);
78
79
        for (let i = 0; i < recentDocs.length; i++) {
80
            const recentDoc = recentDocs[i];
81
82
            structure.push({
83
                label: `${recentDoc}`,
84
                trigger: "openRecentDocs",
85
                triggerData: i,
86
                accelerator: `CommandOrControl+Shift+${i}`
87
            });
88
        }
89
90
        return structure;
91
    }
92
93
    rebuildMenu() {
94
        this.menuStructure = _.cloneDeep(menuStructure);
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable menuStructure seems to be never declared. If this is a global, consider adding a /** global: menuStructure */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
95
        this._rebuildMenu(this.menuStructure);
96
97
        let menu = Menu.buildFromTemplate(this.menuStructure);
0 ignored issues
show
Bug introduced by
The variable Menu seems to be never declared. If this is a global, consider adding a /** global: Menu */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
98
        Menu.setApplicationMenu(menu);
99
    }
100
101
    onRecentDocsChange(newVal, oldVal) {
0 ignored issues
show
Unused Code introduced by
The parameter oldVal is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter newVal is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
102
        this.rebuildMenu();
103
    }
104
}
105